home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
oasis
/
oasis1-1.lha
/
oasis-1.1
/
load.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-05-01
|
5KB
|
170 lines
/*==========================================================================*
Oasis Alpha Version 1.1 (C) Copyright 1992 Fah-Chun Cheong
Revised: 5/1/92 by: fcc@eecs.umich.edu and The University of Michigan
------------------------------------------------------------------------
Permission to use, copy, modify, distribute, sell and resell Oasis Alpha
software and its documentation for any purpose and without fee is hereby
granted, provided that the authorship be appropriately credited and
acknowledged, and that the above copyright notice appear in all copies
and both the copyright notice and this permission notice appear in
supporting documentation. The author makes no representations about the
suitability of this software for any purpose. It is provided "as is"
without express or implied warranty. Oasis Alpha is free, caveat emptor!
------------------------------------------------------------------------
To request Oasis Alpha source code: oasis-alpha-request@eecs.umich.edu
To enroll in the mailing list: oasis-alpha-request@eecs.umich.edu
To send bug reports: oasis-alpha-bugs@eecs.umich.edu
To discuss openly all matters Oasis: oasis-alpha@eecs.umich.edu
*==========================================================================*/
#include <sys/types.h>
#include <stdio.h>
#ifdef NeXT
#include <sys/loader.h>
#define FILHDR struct mach_header
#define FILHSZ sizeof(FILHDR)
#define SGTHDR struct segment_command
#define SGTHSZ sizeof(SGTHDR)
#define SCNHDR struct section
#define SCNHSZ sizeof(SCNHDR)
#define TXTHDR FILHSZ + SGTHSZ
#define DATHDR FILHSZ + SGTHSZ + SCNHSZ
#endif NeXT
#ifdef sparc
#include <a.out.h>
#define FILHDR struct exec
#define FILHSZ sizeof(FILHDR)
#endif sparc
#if defined(apollo) || defined(mips) || defined(_AIX)
#include <a.out.h>
#ifndef FILHDR
#define FILHDR struct filhdr
#endif FILHDR
#ifndef FILHSZ
#define FILHSZ sizeof(FILHDR)
#endif FILHSZ
#ifndef SCNHDR
#define SCNHDR struct scnhdr
#endif SCNHDR
#ifndef SCNHSZ
#define SCNHSZ sizeof(SCNHDR)
#endif SCNHSZ
#define TXTHDR(fh,i) FILHSZ + fh.f_opthdr + SCNHSZ*i
#define DATHDR(fh,i) FILHSZ + fh.f_opthdr + SCNHSZ*i
#ifdef apollo
#define TEXT ".wtext"
#else
#define TEXT ".text"
#endif apollo
#define DATA ".data"
#endif apollo || mips || _AIX
u_long *load_object(name, tseg, dseg)
char *name;
u_long *tseg;
u_long *dseg;
{
FILE *fp;
int tsegsz = 0;
int dsegsz = 0;
int i;
if ((fp = fopen(name, "r")) == NULL) {
perror("opening object file");
exit(-1);
}
#if defined(NeXT)
{
SCNHDR sh;
fseek(fp, TXTHDR, 0);
fread(&sh, SCNHSZ, 1, fp);
fseek(fp, sh.offset, 0);
fread(tseg, sh.size, 1, fp);
tsegsz = sh.size;
fseek(fp, DATHDR, 0);
fread(&sh, SCNHSZ, 1, fp);
fseek(fp, sh.offset, 0);
fread(dseg, sh.size, 1, fp);
dsegsz = sh.size;
}
#endif NeXT
#if defined(sparc)
{
FILHDR fh;
fread(&fh, FILHSZ, 1, fp);
fseek(fp, N_TXTOFF(fh), 0);
fread(tseg, fh.a_text, 1, fp);
tsegsz = fh.a_text;
fseek(fp, N_DATOFF(fh), 0);
fread(dseg, fh.a_data, 1, fp);
dsegsz = fh.a_data;
}
#endif sparc
#if defined(apollo) || defined(mips) || defined(_AIX)
{
FILHDR fh;
SCNHDR sh;
fread(&fh, FILHSZ, 1, fp);
#ifdef LOAD_BUG
printf("%d sections in COFF file.\n", fh.f_nscns);
#endif LOAD_BUG
for (i = 0; i < fh.f_nscns; i++) {
fseek(fp, FILHSZ + fh.f_opthdr + SCNHSZ*i, 0);
fread(&sh, SCNHSZ, 1, fp);
#ifdef LOAD_BUG
printf("Section #%d: \"%s\" (%d bytes)\n", i, sh.s_name, sh.s_size);
#endif LOAD_BUG
if (strcmp(sh.s_name, TEXT) == 0) {
fseek(fp, TXTHDR(fh, i), 0);
fread(&sh, SCNHSZ, 1, fp);
fseek(fp, sh.s_scnptr, 0);
fread(tseg, sh.s_size, 1, fp);
tsegsz = sh.s_size;
}
if (strcmp(sh.s_name, DATA) == 0) {
fseek(fp, DATHDR(fh, i), 0);
fread(&sh, SCNHSZ, 1, fp);
fseek(fp, sh.s_scnptr, 0);
fread(dseg, sh.s_size, 1, fp);
dsegsz = sh.s_size;
}
}
}
#endif apollo || mips || _AIX
fclose(fp);
#ifdef LOAD_BUG
if (tsegsz > 0) printf("Text Section (%d bytes):", tsegsz);
for (i = 0; i < tsegsz / 4; i++) {
if (i % 4 == 0) printf("\n[%08x] ", tseg + i);
printf("%08x ", tseg[i]);
}
printf("\n");
if (dsegsz > 0) printf("Data Section (%d bytes):", dsegsz);
for (i = 0; i < dsegsz / 4; i++) {
if (i % 4 == 0) printf("\n[%08x] ", dseg + i);
printf("%08x ", dseg[i]);
}
printf("\n");
#endif LOAD_BUG
#ifdef _AIX
return dseg;
#else
return tseg;
#endif _AIX
}